package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class CompanyServiceImpl implements CompanyService {
    /**
     * 1.首先去判断缓存reids里面是否有数据
     * 2.如果没有，则去查询数据库，然后将数据存入缓存redis
     * 3.如果有，则直接查询缓存
     *
     * @return
     */
    @Override
    public  CompanyModel getCompanyModel() {
        setTimer();//定时器
        final CompanyModel[] companyModel = {new CompanyModel()};

        for (int i = 0; i < 500; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = setRedis();
                }
            }).start();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return companyModel[0];

    }


    private synchronized CompanyModel setRedis() {
        //        判断缓存redis里面是否有key值
        Jedis jedis = new Jedis();
        Boolean isHave = jedis.exists("logo");

        CompanyModel companyModel = new CompanyModel();
        if (isHave) {
//            从redis取数据
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            System.out.println("redis");
        } else {
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
//            jedis.set("logo", companyModel.getLogo());
//            jedis.expire("logo",100);
            jedis.setex("logo", 100, companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("address", companyModel.getAddress());
            jedis.set("version", companyModel.getVersion());
            System.out.println("mysql");
        }
        return companyModel;
    }

    //        写一个定时器：每天晚上零点将redis的key清空，然后重新查询
    private void setTimer() {
        Timer timer = new Timer();
//        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        long period = 1000 * 60 * 24;//魔法数字
        try {
            parse = simpleDateFormat.parse("2021-3-16 0:0:0");
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(2021, 3, 16, 0, 0, 0);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                deleteRedis();
                setRedis();

            }
        }, parse, period);

    }

    private void deleteRedis() {
        Jedis jedis = new Jedis();
        jedis.del("logo");
    }


    /**
     * 只要用户管理员修改，删除，新增了数据，都应该去删除redis的key值
     *
     * @param companyModel
     * @return
     */
    public String add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.add(companyModel);
        if (result > 0) {
            Jedis jedis = new Jedis();
            jedis.del("logo");
        }
        return result > 0 ? "success" : "error";
    }
}
